//
//  ViewController.m
//  TTWaterFlower
//
//  Created by 田涛 on 2017/3/13.
//  Copyright © 2017年 Lemon. All rights reserved.
//

#import "ViewController.h"
#import "TTWaterflowerView.h"
#import "WaterflowerViewCell.h"
#import "Model.h"
#import "MJExtension.h"
#import <AFNetworking.h>

@interface ViewController () <TTWaterflowerViewDelegate,TTWaterflowerViewDataSource>
@property (nonatomic, strong) NSMutableArray *dataArr;
@property (nonatomic, strong) TTWaterflowerView *waterflowerView;
@end

@implementation ViewController

#pragma mark - 懒加载
- (NSMutableArray *)dataArr {
    if (!_dataArr) {
        self.dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    NSString *plistPath = [[NSBundle mainBundle] pathForResource:@"demo.plist" ofType:nil];
    NSArray *dataArr = [NSArray arrayWithContentsOfFile:plistPath];
    for (NSDictionary *dic in dataArr) {
        Model *model = [[Model alloc] init];
        [model setValuesForKeysWithDictionary:dic];
        [self.dataArr addObject:model];
    }
    
    
    
    TTWaterflowerView *waterflowerView = [[TTWaterflowerView alloc] init];
    waterflowerView.frame = self.view.bounds;
    waterflowerView.delegate = self;
    waterflowerView.dataSource = self;
    [self.view addSubview:waterflowerView];
    self.waterflowerView = waterflowerView;
    
}


#pragma mark - delegate
- (CGFloat)waterflowerView:(TTWaterflowerView *)waterflowerView heightAtIndex:(NSUInteger)index {
    Model *model = _dataArr[index];
    return waterflowerView.cellWidth * model.h / model.w;
}

- (CGFloat)waterflowerView:(TTWaterflowerView *)waterflowerView marginForType:(TTWaterflowerViewMarginType)type {
    switch (type) {
        case TTWaterflowerViewMarginTypeTop:
        case TTWaterflowerViewMarginTypeLeft:
        case TTWaterflowerViewMarginTypeBottom:
        case TTWaterflowerViewMarginTypeRight:
        case TTWaterflowerViewMarginTypeColumn:
            return 10;
            break;
            
        default:
            return 20;
            break;
    }
}

- (void)waterflowerView:(TTWaterflowerView *)waterflowerView didSelectedAtIndex:(NSUInteger)index {
    NSLog(@"点击了第%ld个cell",(unsigned long)index);
}

#pragma mark - dataSource
- (NSUInteger)numberOfCellsInWaterflowerView:(TTWaterflowerView *)waterflowerView {
    return _dataArr.count;
}

- (NSUInteger)numberOfColumnsInWaterflowerView:(TTWaterflowerView *)waterflowerView {
    return 2;
}

- (TTWaterflowerViewCell *)waterflowerView:(TTWaterflowerView *)waterflowerView cellAtIndex:(NSUInteger)index {
    WaterflowerViewCell *cell = [WaterflowerViewCell cellWithWaterflowerView:waterflowerView];
    cell.model = _dataArr[index];
    return cell;
}



@end
